package com.astn.adapter.booking;

import java.util.ArrayList;

import adstn.dialoglib.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.astn.bks.DiaBed;
import com.astn.bks.R;
import com.astn.bks.util.BksConstants;
import com.astn.components.ActionItem;
import com.astn.components.QuickAction;
import com.astn.components.QuickAction.OnActionItemClickListener;
import com.astn.entity.booking.NameValueHolder;
import com.astn.entity.booking.QuoteBed;

public class PBedAdapter extends BaseAdapter {
	private ArrayList<QuoteBed> lstItems = new ArrayList<QuoteBed>();
	private Context context;
	LayoutInflater oInflater;
	public ImageButton firstButton;
	private int lsvBedSelectPos;
	private QuickAction bedQuickAction;
	private QuickAction facilityQuickAction;
	private ActionItem aiAdd;
	private ActionItem aiEdit;
	private ActionItem aiDelete;
	private ActionItem aiClear;
	private QuoteBed currentBed;

	public PBedAdapter(Context context, ArrayList<QuoteBed> lstItems) {
		this.context = context;
		this.lstItems = lstItems;
		oInflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lstItems.size() + 1;
	}

	@Override
	public QuoteBed getItem(int position) {
		if (position > 0) {
			return lstItems.get(position - 1);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0L;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		NameValueHolder holder;
		if (convertView == null) {
			if (position > 0) {
				convertView = oInflater.inflate(R.layout.namevalueitem, null);
				holder = new NameValueHolder();
				holder.txvName = (TextView) convertView
						.findViewById(R.id.txvName);
				holder.txvValue = (TextView) convertView
						.findViewById(R.id.txvValue);

				// holder.status.
				convertView.setTag(holder);
			} else {
				holder = new NameValueHolder();
				convertView = oInflater.inflate(R.layout.firstitem, null);
				convertView.setTag(holder);
//				firstButton = (ImageButton) convertView
//						.findViewById(R.id.btnFirstItem);
				aiAdd = new ActionItem(BksConstants.ADD, "", context
						.getResources().getDrawable(R.drawable.ic_add));
				aiEdit = new ActionItem(BksConstants.EDIT, "", context
						.getResources().getDrawable(R.drawable.ic_edit));
				aiDelete = new ActionItem(BksConstants.DELETE, "", context
						.getResources().getDrawable(R.drawable.ic_delete));
				aiClear = new ActionItem(BksConstants.CLEAR, "", context
						.getResources().getDrawable(R.drawable.ic_delete));
				// convertView=oInflater.inflate(R.layout., root)
				// firstButton.setOnClickListener(new OnClickListener() {
				//
				// @Override
				// public void onClick(View v) {
				// lsvBed1Click();
				// }
				//
				// });
			}
		} else {
			holder = (NameValueHolder) convertView.getTag();
		}
		if (position > 0) {
			try {
				holder.txvName.setText(lstItems.get(position - 1).getName());
				holder.txvValue.setText(String.valueOf(lstItems.get(
						position - 1).getQuantity()));

			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		// holder.txvInfo.setText(lstItems.get(position).getCountryid());
		return convertView;
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return 0;
		}
		return 1;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	// private class BedActionItemSelectedListener implements
	// OnActionItemClickListener
	//
	// {
	//
	// @Override
	// public void onItemClick(QuickAction source, int pos, int actionId) {
	// switch (actionId) {
	// case BksConstants.ADD:
	// Intent iA = new Intent(context, DiaBed.class);
	// iA.putExtra(BksConstants.EXTRA_STATE, BksConstants.ADD);
	// // startActivityForResult(iA, BksConstants.REQUEST_ADD_BED);
	// break;
	// case BksConstants.EDIT:
	// Intent iE = new Intent(context, DiaBed.class);
	// iE.putExtra(BksConstants.EXTRA_STATE, BksConstants.EDIT);
	// if (lsvBedSelectPos > 0) {
	// iE.putExtra(BksConstants.EXTRA_BED,
	// lstItems.get(lsvBedSelectPos - 1));
	// // startActivityForResult(iE,
	// // BksConstants.REQUEST_EDIT_BED);
	// }
	// break;
	// case BksConstants.DELETE:
	// if (lsvBedSelectPos > 0) {
	// lstItems.remove(lsvBedSelectPos - 1);
	// notifyDataSetChanged();
	// }
	// break;
	// case BksConstants.CLEAR:
	// if (Dialog.showYesNoDialog(context,
	// "Do you really want to clear?")) {
	// lstItems = new ArrayList<QuoteBed>();
	// notifyDataSetChanged();
	// }
	// break;
	// default:
	// break;
	// }
	//
	// }
	//
	// }
	//
	// private void lsvBed1Click() {
	// bedQuickAction = new QuickAction(context);
	// bedQuickAction.addActionItem(aiAdd);
	// bedQuickAction.addActionItem(aiClear);
	// bedQuickAction
	// .setOnActionItemClickListener(new BedActionItemSelectedListener());
	// bedQuickAction.show(firstButton);
	// }

}
